package boostarter;

import java.awt.*;

import javax.swing.*;

import controller.Poker;
import poker.InputButtonBS;
import poker.PlayerInfo;
import poker.Table;
import poker.WAdapt;
import view.PokerView;

public class Bootstarter {
	
	JFrame theFrame;
	
	JTextField inTokenCount;
	JTextField inFee;
	JTextField inPlayer1;
	JTextField inPlayer2;
	JTextField inPlayer3;
	JTextField inPlayer4;
	
	JComboBox<String> type1;
	JComboBox<String> type2;
	JComboBox<String> type3;
	JComboBox<String> type4;

	public static void main(String[] args) {
		Bootstarter bs = new Bootstarter();
		bs.initiate();
	}
	
	public void initiate() {
		theFrame  = new JFrame("Poker");
		theFrame.addWindowListener(new WAdapt());
		theFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		String[] playerTypes = new String[] { "Human", "None", "Bot" };
		
		inTokenCount = new JTextField("100");
		inFee = new JTextField("1");
		inPlayer1 = new JTextField("Nazwa gracza 1");
		inPlayer2 = new JTextField("Nazwa gracza 2");
		inPlayer3 = new JTextField("Nazwa gracza 3");
		inPlayer4 = new JTextField("Nazwa gracza 4");
		
		type1 = new JComboBox<String>(playerTypes);
		type2 = new JComboBox<String>(playerTypes);
		type3 = new JComboBox<String>(playerTypes);
		type4 = new JComboBox<String>(playerTypes);
		
		InputButtonBS enter = new InputButtonBS(this, "Graj!");
		enter.setAlignmentX((float)0.5);
		
		JPanel mainPanel = new JPanel();
		JPanel topPanel = new JPanel();
		JPanel playerPanel1 = new JPanel();
		JPanel playerPanel2 = new JPanel();
		JPanel playerPanel3 = new JPanel();
		JPanel playerPanel4 = new JPanel();
		topPanel.setLayout(new GridLayout(1, 2));
		playerPanel1.setLayout(new GridLayout(1, 2));
		playerPanel2.setLayout(new GridLayout(1, 2));
		playerPanel3.setLayout(new GridLayout(1, 2));
		playerPanel4.setLayout(new GridLayout(1, 2));
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
		mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		topPanel.add(inTokenCount);
		topPanel.add(inFee);
		
		playerPanel1.add(inPlayer1);
			playerPanel1.add(type1);
		playerPanel2.add(inPlayer2);
			playerPanel2.add(type2);
		playerPanel3.add(inPlayer3);
			playerPanel3.add(type3);
		playerPanel4.add(inPlayer4);
			playerPanel4.add(type4);
			
		mainPanel.add(topPanel);
		mainPanel.add(playerPanel1);
		mainPanel.add(playerPanel2);
		mainPanel.add(playerPanel3);
		mainPanel.add(playerPanel4);
		mainPanel.add(enter);
		theFrame.add(mainPanel);
		theFrame.setBounds(100, 100, 250, 250);
		theFrame.setVisible(true);
		theFrame.setResizable(false);
	}

	public void checkInput() {
		int tokens = 0;
		int fee = 0;
		int playCount = 0;
		try {
			tokens = Integer.parseInt(inTokenCount.getText());
		}
		catch(Exception e) {
			showError("Wprowadź poprawną liczbę tokenów!");
			return;
		}
		try {
			fee = Integer.parseInt(inFee.getText());
		}
		catch(Exception e) {
			showError("Wprowadź poprawną stawkę!");
			return;
		}
		if((String)type1.getSelectedItem() != "None") playCount++;
		if((String)type2.getSelectedItem() != "None") playCount++;
		if((String)type3.getSelectedItem() != "None") playCount++;
		if((String)type4.getSelectedItem() != "None") playCount++;
		if(playCount == 0) {
			showError("A kto ma niby grać?");
			return;
		}
		PlayerInfo[] players = new PlayerInfo[playCount];
		playCount = 0;
		if((String)type1.getSelectedItem() != "None") {
			players[playCount] = new PlayerInfo();
			if((String)type1.getSelectedItem() == "Bot") {
				players[playCount].isBot = true;
			} else {
				players[playCount].isBot = false;
			}
			players[playCount].name = inPlayer1.getText();
			playCount++;
		}
		if((String)type2.getSelectedItem() != "None") {
			players[playCount] = new PlayerInfo();
			if((String)type2.getSelectedItem() == "Bot") {
				players[playCount].isBot = true;
			} else {
				players[playCount].isBot = false;
			}
			players[playCount].name = inPlayer2.getText();
			playCount++;
		}
		if((String)type3.getSelectedItem() != "None") {
			players[playCount] = new PlayerInfo();
			if((String)type3.getSelectedItem() == "Bot") {
				players[playCount].isBot = true;
			} else {
				players[playCount].isBot = false;
			}
			players[playCount].name = inPlayer3.getText();
			playCount++;
		}
		if((String)type4.getSelectedItem() != "None") {
			players[playCount] = new PlayerInfo();
			if((String)type4.getSelectedItem() == "Bot") {
				players[playCount].isBot = true;
			} else {
				players[playCount].isBot = false;
			}
			players[playCount].name = inPlayer4.getText();
			playCount++;
		}
		try {
			Table theTable = new Table(players, fee, tokens);
			PokerView pv = new PokerView(playCount);
			Poker p = new Poker(theTable);
			pv.setController(p);
			theTable.setView(pv);
			theTable.forceDataFeed();
			theFrame.dispose();
		}
		catch(Exception e) {
			showError(e.getMessage());
			e.printStackTrace();;
			return;
		}
		
	}
	
	public void showError(String str) {
		JOptionPane.showMessageDialog(null, str, "Błąd", JOptionPane.ERROR_MESSAGE);
	}
}
